/*
 * Copyright (C) 2010 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.mjl.factory;

import java.util.*;

import android.os.*;

import com.google.code.mjl.*;
import com.google.code.mjl.Log.*;
import com.google.code.mjl.impl.*;
import com.google.common.collect.*;

/**
 * Assert is mapped to fatal. If you want wtf methods to show the carrect
 * stacktrace, you have to use synchronous logging, otherwise only stacktraces
 * of the logging thread will be shown.
 */
public class AndroidLogAdapterFactory implements LogAdapterFactory {

	private static final int FROYO_API_LEVEL = 8;
	private static final EnumMap<Log.Level, Integer> LEVEL_MAPPING = Maps.newEnumMap(Log.Level.class);

	static {
		LEVEL_MAPPING.put(Log.Level.TRACE, android.util.Log.VERBOSE);
		LEVEL_MAPPING.put(Log.Level.DEBUG, android.util.Log.DEBUG);
		LEVEL_MAPPING.put(Log.Level.INFO, android.util.Log.INFO);
		LEVEL_MAPPING.put(Log.Level.WARN, android.util.Log.WARN);
		LEVEL_MAPPING.put(Log.Level.ERROR, android.util.Log.ERROR);
		LEVEL_MAPPING.put(Log.Level.FATAL, android.util.Log.ASSERT);
	}

	private final LogAdapter logAdapter;

	public AndroidLogAdapterFactory(String tag) {
		logAdapter = new AndroidLogAdapter(tag);
	}

	@Override
	public LogAdapter create(String categoryName) {
		return logAdapter;
	}

	@Override
	public void shutdown() {
	}

	private static class AndroidLogAdapter implements LogAdapter {
		private final String tag;

		public AndroidLogAdapter(String tag) {
			this.tag = tag;
		}

		@Override
		public boolean isEnabled(Level level) {
			return android.util.Log.isLoggable(tag, LEVEL_MAPPING.get(level));
		}

		@Override
		public void log(LogEvent event) {
			switch (event.level()) {
			case TRACE:
				if (event.throwable() == null) {
					android.util.Log.v(tag, event.message());
				} else {
					android.util.Log.v(tag, event.message(), event.throwable());
				}
				break;
			case DEBUG:
				if (event.throwable() == null) {
					android.util.Log.d(tag, event.message());
				} else {
					android.util.Log.d(tag, event.message(), event.throwable());
				}
				break;
			case INFO:
				if (event.throwable() == null) {
					android.util.Log.i(tag, event.message());
				} else {
					android.util.Log.i(tag, event.message(), event.throwable());
				}
				break;
			case WARN:
				if (event.throwable() == null) {
					android.util.Log.w(tag, event.message());
				} else {
					android.util.Log.w(tag, event.message(), event.throwable());
				}
				break;
			case ERROR:
				if (event.throwable() == null) {
					android.util.Log.e(tag, event.message());
				} else {
					android.util.Log.e(tag, event.message(), event.throwable());
				}
				break;
			case FATAL:
				if (Build.VERSION.SDK_INT >= FROYO_API_LEVEL) {
					if (event.throwable() == null) {
						android.util.Log.wtf(tag, event.message());
					} else {
						android.util.Log.wtf(tag, event.message(), event.throwable());
					}
				} else {
					if (event.throwable() == null) {
						android.util.Log.e(tag, event.message());
					} else {
						android.util.Log.e(tag, event.message(), event.throwable());
					}
				}
				break;
			default:
				throw new IllegalStateException("Unknown level: " + event.level());
			}
		}
	}
}
